<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2020/10/19
 * Time: 16:01
 */

namespace app\astrologer\controller;

use think\worker\Server;

/**
 * Class Worker
 * @package app\astrologer\controller
 */
class Worker extends Server
{
    protected $socket = 'websocket://0.0.0.0:9999';

    /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $data)
    {
        print('onMessage:' . $data . PHP_EOL);
        $d = json_decode($data, true);
        var_dump($d);
        $connection->send('我收到你的信息了:' . $data);
    }

    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection)
    {
        var_dump($connection);
        print('连接建立' . PHP_EOL);
    }

    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection)
    {
        print('连接断开' . PHP_EOL);
    }

    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg)
    {
        print('发生错误:' . $msg . PHP_EOL);
        echo "error $code $msg\n";
    }

    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker)
    {
        print('进程启动' . PHP_EOL);
    }
}



